/*-
 * Copyright (c) 2009 Paulo Vizcaíno
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of copyright holders nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <iostream>
#include "sprite.h"

Sprite::Sprite(int frames) {
   list = new Frame*[frames];
   state = 0;
   numframes = frames;
   cont = 0;
   x = 0;
   y = 0;   
   xvelocity = 0; 
   yvelocity = 0;
   state2 = 10;
}

Sprite::~Sprite () {
   delete list;
}

void Sprite::set_velocity(int _x, int _y) {
   yvelocity = _y;
   xvelocity = _x;
}

void Sprite::set_position(int _x, int _y) {
	x = _x;
	y = _y;
}

void Sprite::add_frame(Frame* F) {
   if (cont >= numframes) {
      std::cout << "Error: Sprite, add_frame: MAX Frames reached" << std::endl;
	  return;
   } else {
   
      list[cont] = F;
      
      cont++;
   }
   w = list[state]->w;
   h = list[state]->h;
}

Frame* Sprite::get_actual_frame() {
    if (cont <= 0) {
        std::cerr << "No frames in Sprite" << std::endl;
        return NULL;
    }
    return list[state];    
}

void Sprite::next_state() {
   if ((state+1) < numframes) {
      state++;
   } else {
      state = 0;
   }
   w = list[state]->w;
   h = list[state]->h;
   
}

void Sprite::previous_state() {
   if (state <= 0) {
      state = 4;
   } else {
      state--;
   }
   w = list[state]->w;
   h = list[state]->h;
}
/*
void Sprite::move(char a, bool b) {

   switch (a) {
      case A_DOWN:
         if (b) {
          yvelocity = VEL;
          
          } else {
          if (yvelocity > 0) yvelocity = 0;
          }
         break;
      case A_UP:
         if (b) {
            yvelocity = -VEL;
         } else {
          if (yvelocity < 0) yvelocity = 0;
         }
         break;
      case A_RIGHT:
         if (b) {
            xvelocity = VEL;
          } else {
           if (xvelocity > 0) xvelocity = 0;
          }
         break;
      case A_LEFT:
         if (b) {
            xvelocity = -VEL;
          } else {
           if (xvelocity < 0) xvelocity = 0;
          }
         break;
      default:
	     ;
   }
}*/

void Sprite::refresh() {
   if (xvelocity > 0) {
      x += xvelocity;   
   } else {
      if (xvelocity < 0) {
         x += xvelocity;  
      }
   }
   
   if (yvelocity > 0) {
      y += yvelocity;
   } else {
      if (yvelocity < 0) {
         y += yvelocity;        
      }
   } 
   /*
   if ((xvelocity == 0) && (yvelocity == 0)) {   
      if (state2 < 15) {
         y+=1;
         state2++;      
      } else {      
         if (state2 < 20) {
            y+=1;   
            state2++;      
         } else {
            if (state2 < 25) {
               y-=1;
               state2++;
            } else {
               if (state2 < 30) {
                  y-=1;
                  state2++;              
               } else {
                  state2 = 10;
               }            
            }        
         }     
      }   
   }*/
}




